package com.bigluo.util.redis.impl;

import com.alibaba.fastjson.JSON;
import com.bigluo.common.enums.ERedisCachePrefix;
import com.bigluo.util.redis.IRedisCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * Created by luojun on 2017/9/5.
 */
@Service
public class RedisCacheServiceImpl implements IRedisCacheService{
    private Logger logger = LoggerFactory.getLogger(RedisCacheServiceImpl.class);
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public <T> void setVal(ERedisCachePrefix prefix, String key, T value) {
        if(prefix == null){
            logger.info("ERedisCachePrefix为空");
            return;
        }
        if(value == null ){
            logger.info("需要缓存的value值为空");
            return;
        }
        if(!StringUtils.hasText(key)) {
            logger.info("需要缓存的key为空");
            return;
        }
        String realKey = assembleRealKey(prefix,key);
        String realVal = JSON.toJSONString(value);
        stringRedisTemplate.opsForValue().set(realKey,realVal);
        logger.info("插入realKey:{},realVal:{}成功",realKey,realVal);
    }

    public <T> void setVal(ERedisCachePrefix prefix, String key, T value, Long timeOut, TimeUnit timeUnit) {
        if(prefix == null){
            logger.info("ERedisCachePrefix为空");
            return;
        }
        if(value == null ){
            logger.info("需要缓存的value值为空");
            return;
        }
        if(!StringUtils.hasText(key)) {
            logger.info("需要缓存的key为空");
            return;
        }
        if(timeUnit == null) {
            timeUnit = TimeUnit.MILLISECONDS;
        }
        if(timeOut == null){
            timeOut=1000l;
        }

        String realKey = assembleRealKey(prefix,key);
        String realVal = JSON.toJSONString(value);
        stringRedisTemplate.opsForValue().set(realKey,realVal,timeOut,timeUnit);
        logger.info("插入realKey:{},realVal:{}成功",realKey,realVal);
    }

    public <T> T getVal(ERedisCachePrefix prefix, String key, Class<T> classType) {
        if(prefix == null){
            logger.info("ERedisCachePrefix为空");
            return null;
        }
        if(!StringUtils.hasText(key)) {
            logger.info("缓存的key为空");
            return null;
        }
        String realKey = assembleRealKey(prefix,key);
        String region = stringRedisTemplate.opsForValue().get(realKey);
        T result = null;
        if(!StringUtils.hasText(region)) {
            logger.info("根据key:{}没有找到对应的value",realKey);
        }else{
            result = JSON.parseObject(region,classType);
        }
        return result;
    }

    public void delVal(ERedisCachePrefix prefix, String key) {
        if(prefix == null){
            logger.info("ERedisCachePrefix为空");
            return;
        }
        if(!StringUtils.hasText(key)) {
            logger.info("缓存的key为空");
            return;
        }
        String realKey = assembleRealKey(prefix,key);
        stringRedisTemplate.delete(realKey);
        logger.info("删除realKey:{}成功",realKey);
    }
    private String assembleRealKey(ERedisCachePrefix prefix,String key) {
        return prefix.getCode()+key;
    }
}
